<?php

class SF_Deliveryoption_Helper_Data extends Mage_Core_Helper_Abstract
{

	protected $customOptionTitleEN		 = 'Delivery Options';
	protected $customOptionTitleRU		 = 'Способы доставки';
    protected $productPackageLenCode     = 'common_package_length';
    protected $productPackageWidCode     = 'common_package_width';
    protected $productPackageHeiCode     = 'common_package_height';
    protected $productWeightCode         = 'common_weight';
    protected $vendorIdCode 		     = 'vendor_id';
    protected $otherShippingTypeCode     = 'common_other_shipping_type';
    protected $defaultShippingTypeCode   = 'common_shipping_type';



	/*lie 2016-03-20*/
    /*product save时自动创建custon option功能*/
    public function addCustomOption($product, $productData)
    {
    	if(empty($productData))
    	{
    		Mage::log('$productData is empty!', null, 'rumall.log');
    		return;
    	}

    	/* 配置项 attribute code */
    	

	    /*获取计算运费的相关值*/
        $productPackageLen    = $productData[$this->productPackageLenCode];
        $productPackageWid    = $productData[$this->productPackageWidCode];
        $productPackageHei    = $productData[$this->productPackageHeiCode];
        $productWeight        = $productData[$this->productWeightCode];
        $vendorId 			  = $productData[$this->vendorIdCode];
        $packageWeight 		  = ($productPackageLen*$productPackageWid*$productPackageHei)/12000;
        $finalProductWeight   = $productWeight > $packageWeight ? $productWeight : $packageWeight;


        /*
		 *  默认物流方式(common_shipping_type)必然要增加在custom option上，
		 *  如果有选择其他的物流方式(common_other_shipping_type)，则追加在后面。
         */

        /*获取common_shipping_type属性在产品中所选的选项值,必选单选项*/
		$defaultShippingType   = $productData[$this->defaultShippingTypeCode];
		$productModel = Mage::getModel('catalog/product');
		$attr = $productModel->getResource()->getAttribute($this->defaultShippingTypeCode);
		$defaultShippingTypeTitleEN = $attr->setStoreId(0)->getSource()->getOptionText($defaultShippingType);
		/*获取common_other_shipping_type属性在产品中所选的所有的选项值，可以为空值，表示一个都不选，只走默认方式*/
		$otherShippingTypes = $productData[$this->otherShippingTypeCode];
		$selectShippingTypesTitleEN = array();
		if(!empty($otherShippingTypes))
		{
			$attr = $productModel->getResource()->getAttribute($this->otherShippingTypeCode);
			foreach ($otherShippingTypes as $key => $value)
			{
				$selectShippingTypesTitleEN[] = $attr->setStoreId(0)->getSource()->getOptionText($value);
			}
		}

		
		/*
		 *  组装options
		 */

		/* 14为专递，则只需默认方式。
		 * 如果没有选其他方式，则只需默认方式。
		 */
		if($defaultShippingType == '14' || empty($otherShippingTypes))
		{
			$optionsEN = array
	        (
	            'title' => $this->customOptionTitleEN,
	            'type' => 'drop_down',
	            'is_require' => 0,
	            'sort_order' => 0,
	            'values' => array
	            (
	            	array
		    		(
		                'title' => $defaultShippingTypeTitleEN . ' (Free)',
		                'price' => 0,
		                'price_type' => 'fixed',
		                'sku' => '',
		                'sort_order' => 0,
		       		),
	            )
	        );
		}
		else
		{
			$optionValuesEN = array();
			$shippingPrices = Mage::helper('shippingquery')->returnAllShippingPrice($vendorId,$finalProductWeight);

			/*先把默认的加进来*/
			$optionValuesEN[] = array
			(
				'title' => $defaultShippingTypeTitleEN . ' (Free)',
                'price' => 0,
                'price_type' => 'fixed',
                'sku' => '',
                'sort_order' => 0,
			);
			foreach ($selectShippingTypesTitleEN as $key => $val)
			{
				$price = $shippingPrices[$val] - $shippingPrices[$defaultShippingTypeTitleEN];
				$price = $price > 0 ? $price : 0;

				$optionValuesEN[] = array
	    		(
	                'title' => $val,
	                'price' => $price,
	                'price_type' => 'fixed',
	                'sku' => '',
	                'sort_order' => 0,
	       		);
			}

			$optionsEN = array
		    (
	            'title' => $this->customOptionTitleEN,
	            'type' => 'drop_down',
	            'is_require' => 0,
	            'sort_order' => 0,
	            'values' => $optionValuesEN,
		    );
		}


		/* 删除之前已存在的Delivery Options */

		$storeId = $product->getStoreId();
		$customOptions = $productData['options'];
		if(!empty($customOptions) && is_array($customOptions))
		{
			
			foreach($product->getOptions() as $key => $option)
			{
				if($option->getTitle() == $this->customOptionTitleEN || $option->getTitle() == $this->customOptionTitleRU)
				{
					$customOptions[$key]['is_delete'] = 1;
				}
			}

            $customOptions[] = $optionsEN;
		}
		else
		{
			$customOptions   = array();
            $customOptions[] = $optionsEN;
		}



		return $customOptions;

    }


    public function vendorAddCustomOption($productId, $productData)
    {
    	
    	if(empty($productData))
    	{
    		Mage::log('$product is empty!', null, 'rumall.log');
    		return;
    	}
    	
    	/* 配置项 attribute code */
    	

	    /*获取计算运费的相关值*/
        $productPackageLen    = $productData[$this->productPackageLenCode];
        $productPackageWid    = $productData[$this->productPackageWidCode];
        $productPackageHei    = $productData[$this->productPackageHeiCode];
        $productWeight        = $productData[$this->productWeightCode];
        $vendorId 			  = $productData[$this->vendorIdCode];
        $packageWeight 		  = ($productPackageLen*$productPackageWid*$productPackageHei)/12000;
        $finalProductWeight   = $productWeight > $packageWeight ? $productWeight : $packageWeight;


        /*
		 *  默认物流方式(common_shipping_type)必然要增加在custom option上，
		 *  如果有选择其他的物流方式(common_other_shipping_type)，则追加在后面。
         */

        /*获取common_shipping_type属性在产品中所选的选项值,必选单选项*/
		$defaultShippingType   = $productData[$this->defaultShippingTypeCode];
		$productModel = Mage::getModel('catalog/product');
		$attr = $productModel->getResource()->getAttribute($this->defaultShippingTypeCode);
		$defaultShippingTypeTitleEN = $attr->setStoreId(0)->getSource()->getOptionText($defaultShippingType);
		/*获取common_other_shipping_type属性在产品中所选的所有的选项值，可以为空值，表示一个都不选，只走默认方式*/
		$otherShippingTypes = $productData[$this->otherShippingTypeCode];
		$selectShippingTypesTitleEN = array();
		if(!empty($otherShippingTypes))
		{
			$attr = $productModel->getResource()->getAttribute($this->otherShippingTypeCode);
			foreach ($otherShippingTypes as $key => $value)
			{
				$selectShippingTypesTitleEN[] = $attr->setStoreId(0)->getSource()->getOptionText($value);
			}
		}

		
		/*
		 *  组装options
		 */

		/* 14为专递，则只需默认方式。
		 * 如果没有选其他方式，则只需默认方式。
		 */
		if($defaultShippingType == '14' || empty($otherShippingTypes))
		{
			$optionsEN = array
	        (
	            'title' => $this->customOptionTitleEN,
	            'type' => 'drop_down',
	            'is_require' => 0,
	            'sort_order' => 0,
	            'values' => array
	            (
	            	array
		    		(
		                'title' => $defaultShippingTypeTitleEN . ' (Free)',
		                'price' => 0,
		                'price_type' => 'fixed',
		                'sku' => '',
		                'sort_order' => 0,
		       		),
	            )
	        );
		}
		else
		{
			$optionValuesEN = array();
			$shippingPrices = Mage::helper('shippingquery')->returnAllShippingPrice($vendorId,$finalProductWeight);

			/*先把默认的加进来*/
			$optionValuesEN[] = array
			(
				'title' => $defaultShippingTypeTitleEN . ' (Free)',
                'price' => 0,
                'price_type' => 'fixed',
                'sku' => '',
                'sort_order' => 0,
			);
			foreach ($selectShippingTypesTitleEN as $key => $val)
			{
				$price = $shippingPrices[$val] - $shippingPrices[$defaultShippingTypeTitleEN];
				$price = $price > 0 ? $price : 0;

				$optionValuesEN[] = array
	    		(
	                'title' => $val,
	                'price' => $price,
	                'price_type' => 'fixed',
	                'sku' => '',
	                'sort_order' => 0,
	       		);
			}

			$optionsEN = array
		    (
	            'title' => $this->customOptionTitleEN,
	            'type' => 'drop_down',
	            'is_require' => 0,
	            'sort_order' => 0,
	            'values' => $optionValuesEN,
		    );
		}






		$product = Mage::getModel('catalog/product')->load($productId);
		$optionArr = array();
		if ($product->getId())
		{
	        foreach ($product->getOptions() as $key => $o)
	        {
	        }
		}
		else
		{
		    Mage::log('This Product does not exist.');
		}


		//return $customOptions;


    }

}